package junior.排序;

import static util.Array.*;

public class 快速排序 {
    public static void main(String[] args) {
        int[] array = creatArrayOneDimensional(100);
        showArray(array);
        quickSortArray(array,0,100-1);
        System.out.println();
        showArray(array);
    }
    public static int[] quickSortArray(int[] array,int start, int end){
        int pivot = array[start];
        int i = start;
        int j = end;
        while (i<j) {
            while ((i<j)&&(array[j]>pivot)) {
                j--;
            }
            while ((i<j)&&(array[i]<pivot)) {
                i++;
            }
            if ((array[i]==array[j])&&(i<j)) {
                i++;
            } else {
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
        if (i-1>start)
            array=quickSortArray(array,start,i-1);
        if (j+1<end)
            array=quickSortArray(array,j+1,end);
        return array;
    }
    public static void showArray(int[] array){
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+"\t");
            if ((i+1)%10 == 0) System.out.println();
        }
    }
}
